package model.base;
import java.util.HashMap;
import model.algorithm.AStar;
import model.algorithm.Bfs;
import model.algorithm.Heurictics;
import model.algorithm.Searcher;


public class SearchAlgorithmFactory {
	private HashMap<String, AlgorithmCreator> algorithms;
	private Heurictics h;
	
	public SearchAlgorithmFactory(){		// C'tor
		algorithms = new HashMap<String, AlgorithmCreator>();	//defines cases for  Algorithm creators according to the string given
		algorithms.put("Bfs", new BfsCreator());
		algorithms.put("AStar", new AStarCreator());	
	}
	
	
	
	public Searcher createAlgorithm(String algorithmNamePlusH)		// will be operated by selectAlgorithm in MyModel which will be operated by UserCommands 
	{
		String[] stringArr = algorithmNamePlusH.split(":");			// will get a String and split it to 2 strings - before and after ':'
		if (stringArr.length>1){
			HeuristicsFactory hf = new HeuristicsFactory();			// an instance of heuristicFactory
			this.h = hf.createHeuristics(stringArr[1]);				// which operates createHeuristics(string)
		}
		
		AlgorithmCreator creator = algorithms.get(stringArr[0]);	//	creates a creator according to the first part of the string devided before
		Searcher searcher = null;
		if (creator != null)  {
			searcher = creator.create(h);							// The creator will now create the right searcher with the heuristic made before
		}
		return searcher;	
	}

	
//////////////////////////////////////////////////////////////////
	
	private interface AlgorithmCreator{								//AlgorithmCreator Interface	
		Searcher create(Heurictics h);
	}
	
	private class BfsCreator implements AlgorithmCreator{			// sort of a AlgorithmCreator - specific for BFS
		@Override
		public Searcher create(Heurictics h) {						// creates an instance of BFS   --- gets a Heuristics h but its null - doesnt use it!
			return new Bfs();
		}
	}
	
	private class AStarCreator implements AlgorithmCreator{			// sort of an AlgorithmCreator - specific for AStar
		@Override
		public Searcher create(Heurictics h) {						// creates an instance of AStar --- Gets a Heuristic h
			return new AStar(h);
		}
	}
	
	
	
}